<?php

namespace App\Admin\Controllers;

use App\Models\Type;
use Encore\Admin\Admin;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\Admin\Tree;
use http\Env\Request;

class TypeController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '影视分类';

    public function index(Content $content)
    {
        $tree = new Tree(new Type());
        $tree->branch(function ($branch) {
            return "{$branch['name']}";
        });
        return $content
            ->header('影视分类')
            ->body($tree);
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Type());

        $grid->column('id', __('Id'));
        $grid->column('name', __('分类名称'));
        $grid->column('icon', __('分类图标'));
        $grid->column('created_at', __('创建时间'));
        $grid->column('updated_at', __('修改时间'));
        $grid->column('isdelete', __('状态'))->replace([-1=>"冻结",0=>"删除",1=>"正常"]);

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Type::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('name', __('分类名称'));
        $show->field('icon', __('分类图标'));
        $show->field('created_at', __('创建时间'));
        $show->field('updated_at', __('修改时间'));
        $show->field('isdelete', __('状态'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Type());
        $form->text('name', __('分类名称'));
        $form->image('icon', __('分类图标'));
        $states = [
            'on'  => ['value' => 1, 'text' => '正常', 'color' => 'success'],
            'off' => ['value' => -1, 'text' => '冻结', 'color' => 'danger'],
        ];
        $form->switch('isdelete', __('状态'))->default(1)->states($states);

        return $form;
    }

}
